<!--
  - Copyright (c) 2019.  武汉中科图灵科技有限公司
  - Date :  2019/1/5
  - Author ：F12 Console=> atob("MzMxNDkxODAwQHFxLmNvbQ==")
  - Version ：1.6.2
  -->

<template>
  <div class="scroll-container" ref="scrollContainer" @wheel.prevent="handleScroll" @touchstart="handleTouchStart" @touchmove="handleTouchMove">
    <div class="scroll-wrapper" ref="scrollWrapper" :style="{top: top + 'px'}">
      <slot></slot>
    </div>
  </div>
</template>

<script>
const delta = 15

export default {
  name: 'scrollBar',
  data() {
    return {
      top: 0,
      startY:0,
    }
  },
  methods: {
    scrollMethod(dis){

      const $container = this.$refs.scrollContainer
      const $containerHeight = $container.offsetHeight
      const $wrapper = this.$refs.scrollWrapper
      const $wrapperHeight = $wrapper.offsetHeight
      if ( dis> 0) {
        this.top = Math.min(0, this.top + dis)
      } else {
        if ($containerHeight - delta < $wrapperHeight) {
          if (this.top < -($wrapperHeight - $containerHeight + delta)) {
            this.top = this.top
          } else {
            this.top = Math.max(this.top + dis, $containerHeight - $wrapperHeight - delta)
          }
        } else {
          this.top = 0
        }
      }
    },
    handleTouchStart(e){
      this.startY = e.touches[0].clientY
    },
    handleTouchMove(e){
      // 兼容移动端事件，待去冗余
      let dis = e.touches[0].clientY - this.startY
      this.scrollMethod(dis)
    },
    handleScroll(e) {
      const eventDelta = e.wheelDelta || -e.deltaY * 3
      this.scrollMethod(eventDelta)
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.scroll-container {
  position: relative;
  width: 100%;
  height: 100%;
  .scroll-wrapper {
    position: absolute;
     width: 100%!important;
  }
}
#app .hideSidebar .sidebar-container, #app .hideSidebar .sidebar-container .el-menu{
  width: 70px !important;
}
</style>
